package com.gliese.sortmap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class SortMap {
	public static void main(String[] args) {
		Map<String, String> yourMap = new HashMap<String, String>();
		yourMap.put("3", "c");
		yourMap.put("4", "d");
		yourMap.put("1", "a");
		yourMap.put("2", "b");

		Map<String, Object> map = new LinkedHashMap<String, Object>();

		List<String> keyList = new ArrayList<String>(yourMap.keySet());
		List<String> valueList = new ArrayList<String>(yourMap.values());
		Set<String> sortedSet = new TreeSet<String>(valueList);

		Object[] sortedArray = sortedSet.toArray();
		int size = sortedArray.length;

		for (int i = 0; i < size; i++) {
			map.put(keyList.get(valueList.indexOf(sortedArray[i])),
					sortedArray[i]);
		}
		Set ref = map.keySet();
		Iterator it = ref.iterator();
		while (it.hasNext()) {
			String i = (String) it.next();
			System.out.println(i + ":" + map.get(i));
		}
	}
}